/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Web.UI;

using DotNetNuke.Common.Utilities;
using DotNetNuke.UI.WebControls;

using DotNetNuke.Modules.Articles.Framework;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Modules.Articles.Common;
using DotNetNuke.ModuleToolkit.Localization;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Editors
{
    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.UI.Editors
    /// Class:      EditorBase
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An Edit Control to Edit sections
    /// </summary>
    /// <history>
    /// 	[cnurse]	04/20/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SectionEditControl : IntegerEditControl
    {

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the ModuleId of the parent Module
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private int GetModuleId()
        {
            int _ModuleId = Null.NullInteger;
            System.Web.UI.Control myParent = this.Parent;
            while (myParent != null)
            {
                if (myParent is ArticlesModuleBase)
                {
                    _ModuleId = ((ArticlesModuleBase)myParent).ModuleId;
                    break;
                }
                else if (myParent is ModuleSettingsBase)
                {
                    _ModuleId = ((ModuleSettingsBase)myParent).ModuleId;
                    break;
                }
                myParent = myParent.Parent;
            }

            return _ModuleId;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Render renders the control in edit mode
        /// </summary>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	04/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderEditMode(System.Web.UI.HtmlTextWriter writer)
        {
            int propValue = Int32.Parse(StringValue);

            //Get the Sections
            List<SectionInfo> sections = SectionController.GetSections(GetModuleId());

            //Render the Select Tag
            ControlStyle.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            //Render the "None Specifed" Option
            string notSpecifiedText = Localization.LocalizeString(this, Constants.TEXT_NotSpecified);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "-1");
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.Write(notSpecifiedText);
            writer.RenderEndTag();

            foreach (SectionInfo section in sections)
            {
                //Add the Value Attribute
                writer.AddAttribute(HtmlTextWriterAttribute.Value, section.SectionId.ToString());

                if ( section.SectionId == propValue)
                    //Add the Selected Attribute
                    writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");

                //Render Option Tag
                writer.RenderBeginTag(HtmlTextWriterTag.Option);
                writer.Write(section.Name);
                writer.RenderEndTag();
            }

            //Close Select Tag
            writer.RenderEndTag();
        }

        public override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            return base.LoadPostData(postDataKey, postCollection);
        }
    }

}
